package com.example.templateapp.util

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_ONE_SHOT
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationManagerCompat
import com.example.templateapp.R

object NotificationUtil {
    private var msgId = 0
    private val channelId = "支付通知"
    private val channelName = "支付通知"

    private lateinit var context: Context
    private lateinit var notificationManager: NotificationManagerCompat

    @RequiresApi(Build.VERSION_CODES.O)
    fun init(context: Context) {
        this.context = context
        notificationManager = NotificationManagerCompat.from(context)
        notificationManager.createNotificationChannel(
            NotificationChannel(
                channelId,
                channelName,
                NotificationManager.IMPORTANCE_HIGH
            )
        )
    }


    @RequiresApi(Build.VERSION_CODES.O)
    fun notifyMsg(title: String, content: String, intent: Intent) {


        val pendingIntent = PendingIntent.getActivity(
            context,
            11,
            intent,
            FLAG_ONE_SHOT
        )

        val notification = Notification.Builder(context, channelId)
            .setContentTitle(title)
            .setContentText(content)
            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher))
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentIntent(pendingIntent)
            .setShowWhen(true)
            .build()
        notificationManager.notify(msgId, notification)

    }


}